package com.friendfeed.api;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Entry {
    private String id;
    private String title;
    private String link;
    private Date published;
    private Date updated;
    private boolean hidden;
    private User user;
    private Service service;
    private List<Comment> comments;
    private List<Like> likes;
    private List<Media> media;
    private Via via;
    private Room room;

    private JSONObject jsonObject;

    public Entry() {
        comments = new ArrayList<Comment>();
        likes = new ArrayList<Like>();
        media = new ArrayList<Media>();
    }

    public Entry(JSONObject jsonObject) throws JSONException, ParseException {
        this.jsonObject = jsonObject;

        id = jsonObject.getString("id");
        title = jsonObject.getString("title");
        link = jsonObject.getString("link");
        if (jsonObject.has("published") && !jsonObject.isNull("published")) {
            published = FriendFeedUtils.parseDate(jsonObject.getString("published"));
        }
        if (jsonObject.has("updated") && !jsonObject.isNull("updated")) {
            updated = FriendFeedUtils.parseDate(jsonObject.getString("updated"));
        }
        hidden = jsonObject.getBoolean("hidden");
        user = new User(jsonObject.getJSONObject("user"));
        service = new Service(jsonObject.getJSONObject("service"));

        // comments
        JSONArray commentsArray = jsonObject.getJSONArray("comments");
        comments = new ArrayList<Comment>(commentsArray.length());
        for (int i = 0; i < commentsArray.length(); i++) {
            comments.add(new Comment(commentsArray.getJSONObject(i)));
        }

        // likes
        JSONArray likesArray = jsonObject.getJSONArray("likes");
        likes = new ArrayList<Like>(likesArray.length());
        for (int i = 0; i < likesArray.length(); i++) {
            likes.add(new Like(likesArray.getJSONObject(i)));
        }

        // media
        JSONArray mediaArray = jsonObject.getJSONArray("media");
        media = new ArrayList<Media>(mediaArray.length());
        for (int i = 0; i < mediaArray.length(); i++) {
            media.add(new Media(mediaArray.getJSONObject(i)));
        }

        if (jsonObject.has("via")) {
            via = new Via(jsonObject.getJSONObject("via"));
        }

        if (jsonObject.has("room")) {
            room = new Room(jsonObject.getJSONObject("room"));
        }
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getLink() {
        return link;
    }

    public Date getPublished() {
        return published;
    }

    public Date getUpdated() {
        return updated;
    }

    public boolean isHidden() {
        return hidden;
    }

    public User getUser() {
        return user;
    }

    public Service getService() {
        return service;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public List<Like> getLikes() {
        return likes;
    }

    public List<Media> getMedia() {
        return media;
    }

    public Via getVia() {
        return via;
    }

    public Room getRoom() {
        return room;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public void setPublished(Date published) {
        this.published = published;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setService(Service service) {
        this.service = service;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public void setLikes(List<Like> likes) {
        this.likes = likes;
    }

    public void setMedia(List<Media> media) {
        this.media = media;
    }

    public void setVia(Via via) {
        this.via = via;
    }

    public void setRoom(Room room) {
        this.room = room;
    }

    public String toString() {
        return "Entry{" +
                "id='" + id + '\'' +
                ", title='" + title + '\'' +
                ", link='" + link + '\'' +
                ", published=" + published +
                ", updated=" + updated +
                ", hidden=" + hidden +
                ", user=" + user +
                ", service=" + service +
                '}';
    }

    public JSONObject getJsonObject() {
        return jsonObject;
    }
}
